<?php
class database{
	var $Conn = '';
	public function Connect(){		
		$this->Conn = mysql_connect(Config::HOST, Config::USERNAME, Config::PASSWORD);		
		mysql_select_db(Config::DATABASE,$this->Conn);
		mysql_query("SET NAMES utf8");
		if(!$this->Conn){					
			die("Không thể kết nối đến database.");
		}
		
	}
	public function Close_Connect(){	
		if($this->Conn != ''){
			mysql_close($this->Conn);
		}					
	}
		
	public function query($Sql){		
		$this->Connect(); 
		if($result=mysql_query($Sql)){						
			return $result;
		}else{
			return 0;	
		}
	}
	
	public function CountRow($Result){
		$Count = 0;
		if(is_null($Result)||$Result==0){
			return 0;
		}		
		return mysql_num_rows($Result);		
	}	

	public function fetch($Result){
		if(is_null($Result)){
			return 0;
		}else{			
			return  mysql_fetch_array($Result);
		}
	}
	public function isInt($int){
		if(!is_numeric($int) || $int<0 || $int>2300000000|| !preg_match('/^[0-9]+$/',$int)){
			return false;	
		}else{
			return true;	
		}
	}
	public function isTinyInt($int){
		if(!is_numeric($int) || $int<0 || strlen($int)>4 || !preg_match('/^[0-9]+$/',$int)){
			return false;	
		}else{
			return true;	
		}
	}
	
	public function isNum($int){
		if(!is_numeric($int)){
			return false;	
		}else{
			return true;	
		}
	}
	public function isChar($str) {
		return preg_match('/^[a-zA-Z0-9_-]+$/',$str);
	}
	public function isText($str) {		
		$str='a'.$str;
		if(strpos($str,"'")>0){
			return false;
		}else{
			return true;
		}		
	}
	public function isLen($str,$min,$max) {
		if(!is_null($str) && $this->isInt($min) && $this->isInt($max)){
			if(strlen($str)>=$min && strlen($str)<=$max){
				return 1;
			}	
		}
		return false;
	}

	function valid($vname) {
		$vname = str_replace("&","&amp;",$vname);// &
		$vname = str_replace(">","&gt;",$vname);// >
		$vname = str_replace("<","&lt;",$vname);// <
		$vname = str_replace("?","&#63;",$vname);// ?
		$vname = str_replace("'","&#039;",$vname);// '
		$vname = str_replace('"','&quot;',$vname);// "	
		return $vname;	
	}
	function unvalid($vname) {
		$vname = str_replace("&amp;","&",$vname);// &
		$vname = str_replace("&gt;",">",$vname);// >
		$vname = str_replace("&lt;","<",$vname);// <
		$vname = str_replace("&#63;","?",$vname);// ?
		$vname = str_replace("&#039;","'",$vname);// '
		$vname = str_replace('&quot;','"',$vname);// "
		return $vname;	
	}
	function Replace($Char1,$Char2,$Str) {		
		return str_replace($Char1,$Char2,$Str);	
	}
	function sqlesc($x) {
   		return "'".mysql_real_escape_string($x)."'";
	}
	// doi tu chuoi co dau sang ko co dau
	public function ConverUnicode($text){		
		$chars = array("a","A","e","E","o","O","u","U","i","I","d", "D","y","Y");		
		$uni[0] = array("á","à","ạ","ả","ã","â","ấ","ầ", "ậ","ẩ","ẫ","ă","ắ","ằ","ặ","ẳ","� �");
		$uni[1] = array("Á","À","Ạ","Ả","Ã","Â","Ấ","Ầ", "Ậ","Ẩ","Ẫ","Ă","Ắ","Ằ","Ặ","Ẳ","� �");
		$uni[2] = array("é","è","ẹ","ẻ","ẽ","ê","ế","ề" ,"ệ","ể","ễ");
		$uni[3] = array("É","È","Ẹ","Ẻ","Ẽ","Ê","Ế","Ề" ,"Ệ","Ể","Ễ");
		$uni[4] = array("ó","ò","ọ","ỏ","õ","ô","ố","ồ", "ộ","ổ","ỗ","ơ","ớ","ờ","ợ","ở","� �");
		$uni[5] = array("Ó","Ò","Ọ","Ỏ","Õ","Ô","Ố","Ồ", "Ộ","Ổ","Ỗ","Ơ","Ớ","Ờ","Ợ","Ở","� �");
		$uni[6] = array("ú","ù","ụ","ủ","ũ","ư","ứ","ừ", "ự","ử","ữ");
		$uni[7] = array("Ú","Ù","Ụ","Ủ","Ũ","Ư","Ứ","Ừ", "Ự","Ử","Ữ");
		$uni[8] = array("í","ì","ị","ỉ","ĩ");
		$uni[9] = array("Í","Ì","Ị","Ỉ","Ĩ");
		$uni[10] = array("đ");
		$uni[11] = array("Đ");
		$uni[12] = array("ý","ỳ","ỵ","ỷ","ỹ");
		$uni[13] = array("Ý","Ỳ","Ỵ","Ỷ","Ỹ");
		
		for($i=0; $i<=13; $i++) {
		$text = str_replace($uni[$i],$chars[$i],$text);
		}

		return $text;
	}	
}
?>